package com.qixin.modules.ea.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.qixin.modules.ea.entity.*;
import com.qixin.modules.ea.mapper.*;
import com.qixin.modules.ea.service.IEaEntertainService;
import com.qixin.modules.ea.service.IEaEntertainServiceCopy;
import com.qixin.modules.ea.vo.ListPageVo;
import com.qixin.modules.eaApplication.entity.EaBusinessApply;
import com.qixin.modules.eaApplication.entity.EaEntertainApply;
import com.qixin.modules.eaApplication.entity.EaHostObject;
import com.qixin.modules.eaApplication.mapper.EaBusinessApplyMapper;
import com.qixin.modules.eaApplication.mapper.EaEntertainApplyMapper;
import com.qixin.modules.eaApplication.mapper.EaHostObjectMapper;
import com.qixin.modules.eaBasic.entity.EaTask;
import com.qixin.modules.eaBasic.mapper.EaTaskMapper;
import com.qixin.modules.eaBasic.util.ChangeIdToNameUtil;
import com.qixin.modules.eaBasic.util.WorkFlowUtil;
import com.qixin.modules.eaBasic.vo.EaTaskVo;
import com.qixin.modules.eaLoan.entity.EaLoanbill;
import com.qixin.modules.eaLoan.mapper.EaLoanbillMapper;
import com.qixin.modules.eaStandard.mapper.EaTrainstandardMapper;
import com.qixin.modules.rzInvoice.entity.EaBill;
import com.qixin.modules.rzInvoice.mapper.EaBillMapper;
import org.apache.shiro.SecurityUtils;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Description: 招待费报销单
 * @Author: qixin-boot
 * @Date:   2021-07-06
 * @Version: V1.0
 */
@Service
public class EaEntertainServiceImplCopy extends ServiceImpl<EaEntertainMapper, EaEntertain> implements IEaEntertainServiceCopy {
	@Autowired
	private EaEntertainMapper eaEntertainMapper;
	@Autowired
	private EaBookDetailMapper eaBookDetailMapper;
	@Autowired
	private EaApplyDetailMapper eaApplyDetailMapper;
	@Autowired
	private EaCostShareMapper eaCostShareMapper;
	@Autowired
	private EaBorrowSubtractMapper eaBorrowSubtractMapper;
	@Autowired
	private EaBillMapper eaBillMapper;
	@Autowired
	private EaTaskMapper eaTaskMapper;
	@Value(value = "${server.fileurl}")
	private String fileurl;
	@Autowired
	private EaLoanbillMapper eaLoanbillMapper;
	@Autowired
	private EaEntertainApplyMapper eaEntertainApplyMapper;
	@Autowired
	private ChangeIdToNameUtil changeIdToNameUtil;
	@Autowired
	private EaTrainstandardMapper eaTrainstandardMapper;
	@Autowired
	private WorkFlowUtil workFlowUtil;
	@Autowired
	private EaEntertainReceptionActivityMapper eaEntertainReceptionActivityMapper;

	@Override
	@Transactional
	public Map<String,Object> saveMain(EaEntertain eaEntertain, List<EaBookDetail> eaBookDetailList, List<EaEntertainApply> eaApplyDetailList, List<EaBorrowSubtract> eaBorrowSubtractList, List<EaEntertainReceptionActivity> eaEntertainReceptionActivities) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
		Object result = FillRuleUtil.executeRule("entertain_ea_code", null);  //获取单据编号
		eaEntertain.setBillNo(result.toString());

		String saveType = eaEntertain.getSaveType();
		if(saveType.equals("add")) {  //add保存,send提交
			eaEntertain.setState(0);   //单据状态:0未提交,1审批中,2已审批,3已退回
		}else{
			eaEntertain.setState(1);   //单据状态:0未提交,1审批中,2已审批,3已退回
		}

		eaEntertain.setId(Uuid32Util.getUUid());  //主键
		eaEntertainMapper.insert(eaEntertain);

		//查询招待费报销单
		//新增待办
		EaTask eaTask = new EaTask();
		eaTask.setId(Uuid32Util.getUUid());
		eaTask.setFormType("8");  //单据类型:1招待费报销单,2借款单,3还款单,4招待费申请单,5出差申请变更单,6招待费申请单,7招待费申请变更单,8招待费报销单
		eaTask.setAuditManId(eaEntertain.getAuditManId());   //审批人id
		eaTask.setAuditMan(eaEntertain.getAuditMan());    //审批人
		//提交类型:保存:add,提交:send
		if(saveType.equals("add")){  //add保存,send提交
			eaTask.setStatus("0");
		}else{
			eaTask.setStatus("1");
		}
		eaTask.setFormId(eaEntertain.getId());    //单据id
		eaTask.setFormCode(eaEntertain.getBillNo());   //单据编号
		eaTask.setMemo(eaEntertain.getCause());    //备注
		eaTask.setTotalFee(eaEntertain.getActualPayAmt()); //总金额
		eaTask.setSort("8");   //排序
		eaTask.setTitle("招待费报销单");  //标题
		eaTask.setSendManId(eaEntertain.getPersonId());    //发送人id
		eaTask.setSendMan(changeIdToNameUtil.getPsnNameById(eaEntertain.getPersonId()));    //发送人
		eaTask.setSendDepartId(eaEntertain.getDeptId());  //发送人部门id
		eaTask.setSendDepart(changeIdToNameUtil.getNameByDepartId(eaEntertain.getDeptId()));   //发送人部门
		eaTask.setSendTime(new Date());    //发送时间
		eaTaskMapper.insert(eaTask);

		if(eaBookDetailList!=null && eaBookDetailList.size()>0) {
			for(EaBookDetail entity:eaBookDetailList) {
				//更新发票使用情况
				String fpid = entity.getFpid();
				EaBill eaBill = eaBillMapper.selectById(fpid);
				String isused = eaBill.getIsused();   //发票是否已使用
				if(isused.equals("1")){
					throw new RuntimeException("发票号码:" + eaBill.getNumber() + "已被使用!");
				}else{
					eaBill.setIsused("1");  //设置发票已使用
					eaBillMapper.updateById(eaBill);
				}

				//外键设置
				entity.setBillId(eaEntertain.getId());
				entity.setId(Uuid32Util.getUUid());
				eaBookDetailMapper.insert(entity);
			}
		}
		if(eaApplyDetailList!=null && eaApplyDetailList.size()>0) {
//			for(EaApplyDetail entity:eaApplyDetailList) {
//				//更新招待费申请单使用情况
//				String businessApplyId = entity.getBusinessApplyId();
//				EaEntertainApply eaEntertainApply = eaEntertainApplyMapper.selectById(businessApplyId);
//				String isused = eaEntertainApply.getIsused();   //招待费申请单是否已使用
//
//				if(isused.equals("1")){
//					throw new RuntimeException("招待费申请单:" + eaEntertainApply.getBillNo() + "已被使用!");
//				}else{
//					eaEntertainApply.setIsused("1");  //设置招待费申请单已使用
//					eaEntertainApplyMapper.updateById(eaEntertainApply);
//				}
//
//				//外键设置
//				entity.setBillId(eaEntertain.getId());
//				entity.setId(Uuid32Util.getUUid());
//				eaApplyDetailMapper.insert(entity);
//			}
		}
		if(eaBorrowSubtractList!=null && eaBorrowSubtractList.size()>0) {
			for(EaBorrowSubtract entity:eaBorrowSubtractList) {
				//更新借款单使用情况
				String loanBillId = entity.getLoanBillId();
				EaLoanbill eaLoanbill = eaLoanbillMapper.selectById(loanBillId);
				String isused = eaLoanbill.getIsused();   //借款单是否已使用
				if(isused.equals("1")){
					throw new RuntimeException("借款单:" + eaLoanbill.getBillNo() + "已被使用!");
				}else{
					eaLoanbill.setIsused("1");  //设置招待费申请单已使用
					//更新借款单信息:来源报销单信息
					eaLoanbill.setSrcbxbillCode(eaEntertain.getBillNo());
					eaLoanbill.setSrcbxbillPk(eaEntertain.getId());
					eaLoanbill.setSrcbxbillStatus(String.valueOf(eaEntertain.getState()));
					eaLoanbillMapper.updateById(eaLoanbill);
				}

				//外键设置
				entity.setBillId(eaEntertain.getId());
				entity.setId(Uuid32Util.getUUid());
				eaBorrowSubtractMapper.insert(entity);
			}
		}
		if(eaEntertainReceptionActivities!=null && eaEntertainReceptionActivities.size()>0) {
			for(EaEntertainReceptionActivity entity:eaEntertainReceptionActivities) {
				//新增活动明细
				entity.setPid(eaEntertain.getId());
				entity.setId(Uuid32Util.getUUid());
				eaEntertainReceptionActivityMapper.insert(entity);
			}
		}

		returnMap.put("id",eaEntertain.getId());
		returnMap.put("code","0");
		returnMap.put("message","保存成功!");
		return returnMap;
	}

	@Override
	@Transactional
	public Map<String,Object> updateMain(EaEntertain eaEntertain,List<EaBookDetail> eaBookDetailList,List<EaEntertainApply> eaApplyDetailList,List<EaBorrowSubtract> eaBorrowSubtractList, List<EaEntertainReceptionActivity> eaEntertainReceptionActivities) {
		Map<String,Object> returnMap = new HashMap<String,Object>();

		//如果picture中有http://localhost:9092/qixin-boot/aaa.png,要处理只保留文件名
		String picture = eaEntertain.getPicture() != null ? eaEntertain.getPicture().toString() : "";
		String picture_final = picture.replaceAll(fileurl,"");;
		//设置最终的图片如:1111.png,2222.png
		eaEntertain.setPicture(picture_final);

		String saveType = eaEntertain.getSaveType();
		if(saveType.equals("add")) {  //add保存,send提交
			EaEntertain eaEntertain1 = eaEntertainMapper.selectById(eaEntertain.getId());
			eaEntertain.setState(eaEntertain1.getState());   //单据状态:0未提交,1审批中,2已审批,3已退回
		}else{
			eaEntertain.setState(1);   //单据状态:0未提交,1审批中,2已审批,3已退回
		}

		eaEntertainMapper.updateById(eaEntertain);

		//先将之前的相关发票状态改成未使用
		//获取原费用明细中的发票id
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("bill_id",eaEntertain.getId());
		List<EaBookDetail> eaBookDetailList1 = eaBookDetailMapper.selectByMap(paramsMap);
		for(EaBookDetail entity:eaBookDetailList1) {
			String fpid = entity.getFpid();  //发票id
			EaBill eaBill = eaBillMapper.selectById(fpid);
			eaBill.setIsused("0");   //设置状态为未使用
			eaBillMapper.updateById(eaBill);
		}

		//获取原费用明细中的招待费申请单id
		Map<String,Object> paramsMap2 = new HashMap<String,Object>();
		paramsMap2.put("bill_id",eaEntertain.getId());
		List<EaApplyDetail> eaApplyDetailList1 = eaApplyDetailMapper.selectByMap(paramsMap2);
		for(EaApplyDetail entity:eaApplyDetailList1) {
			String businessApplyId = entity.getBusinessApplyId();  //招待费申请单id
			EaEntertainApply eaEntertainApply = eaEntertainApplyMapper.selectById(businessApplyId);
			eaEntertainApply.setIsused("0");   //设置状态为未使用
			eaEntertainApplyMapper.updateById(eaEntertainApply);
		}

		//获取原费用明细中的借款单id
		Map<String,Object> paramsMap3 = new HashMap<String,Object>();
		paramsMap3.put("bill_id",eaEntertain.getId());
		List<EaBorrowSubtract> eaBorrowSubtractList1 = eaBorrowSubtractMapper.selectByMap(paramsMap3);
		for(EaBorrowSubtract entity:eaBorrowSubtractList1) {
			String fpid = entity.getLoanBillId();  //借款单id
			EaLoanbill eaLoanbill = eaLoanbillMapper.selectById(fpid);
			eaLoanbill.setIsused("0");   //设置状态为未使用
			eaLoanbillMapper.updateById(eaLoanbill);
		}

		//1.先删除子表数据
		eaBookDetailMapper.deleteByMainId(eaEntertain.getId());
		eaApplyDetailMapper.deleteByMainId(eaEntertain.getId());
		eaCostShareMapper.deleteByMainId(eaEntertain.getId());
		eaBorrowSubtractMapper.deleteByMainId(eaEntertain.getId());
		Map<String,Object> activitiParams = new HashMap<String,Object>();
		activitiParams.put("pid",eaEntertain.getId());
		eaEntertainReceptionActivityMapper.deleteByMap(activitiParams);

		//修改待办
		Map<String,Object> paramsMap4 = new HashMap<String,Object>();
		paramsMap4.put("form_id",eaEntertain.getId());
		List<EaTask> eaTaskList = eaTaskMapper.selectByMap(paramsMap4);
		for (EaTask eaTask:eaTaskList) {
			eaTask.setAuditManId(eaEntertain.getAuditManId());   //审批人id
			eaTask.setAuditMan(eaEntertain.getAuditMan());    //审批人
			//提交类型:保存:add,提交:send
			if(saveType.equals("add")){  //add保存,send提交
				eaTask.setStatus("0");
			}else{
				eaTask.setStatus("1");
			}
			eaTask.setFormId(eaEntertain.getId());    //单据id
			eaTask.setFormCode(eaEntertain.getBillNo());   //单据编号
			eaTask.setMemo(eaEntertain.getCause());    //备注
			eaTask.setTotalFee(eaEntertain.getActualPayAmt()); //总金额
			eaTask.setSendManId(eaEntertain.getPersonId());    //发送人id
			eaTask.setSendMan(changeIdToNameUtil.getPsnNameById(eaEntertain.getPersonId()));    //发送人
			eaTask.setSendDepartId(eaEntertain.getDeptId());  //发送人部门id
			eaTask.setSendDepart(changeIdToNameUtil.getNameByDepartId(eaEntertain.getDeptId()));   //发送人部门
			eaTask.setSendTime(new Date());    //发送时间
			eaTaskMapper.updateById(eaTask);
		}

		//2.子表数据重新插入
		if(eaBookDetailList!=null && eaBookDetailList.size()>0) {
			for(EaBookDetail entity:eaBookDetailList) {
				//更新发票使用情况
				String fpid = entity.getFpid();
				EaBill eaBill = eaBillMapper.selectById(fpid);
				String isused = eaBill.getIsused();   //发票是否已使用
				if(isused.equals("1")){
					throw new RuntimeException("发票号码:" + eaBill.getNumber() + "已被使用!");
				}else{
					eaBill.setIsused("1");  //设置发票已使用
					eaBillMapper.updateById(eaBill);
				}

				//外键设置
				entity.setBillId(eaEntertain.getId());
				entity.setId(Uuid32Util.getUUid());
				eaBookDetailMapper.insert(entity);
			}
		}
		if(eaApplyDetailList!=null && eaApplyDetailList.size()>0) {
//			for(EaApplyDetail entity:eaApplyDetailList) {
//				//更新招待费申请单使用情况
//				String businessApplyId = entity.getBusinessApplyId();
//				EaEntertainApply eaEntertainApply = eaEntertainApplyMapper.selectById(businessApplyId);
//				String isused = eaEntertainApply.getIsused();   //招待费申请单是否已使用
//
//				//判断报销金额是否符合标准:商务接待-600,公务接待150
//				String receptionType = eaEntertainApply.getReceptionType(); //招待类型
//				Map<String, Object> dictItem_sw = eaTrainstandardMapper.getDictItemById("1410435522261612223");
//				Map<String, Object> dictItem_gw = eaTrainstandardMapper.getDictItemById("1410435522261612221");
//				if(receptionType.equals("1410435522261612221")){  //公务接待
//					String item_value = EmptyUtil.checkNull(dictItem_gw.get("item_value"));
//					if(new BigDecimal(item_value).compareTo(eaEntertainApply.getAmount()) > 0){
//						throw new RuntimeException("报销金额:" + item_value + "超过公务接待报销标准:" + eaEntertainApply.getAmount());
//					}
//				}else if(receptionType.equals("1410435522261612223")){  //商务接待
//					String item_value = EmptyUtil.checkNull(dictItem_sw.get("item_value"));
//					if(new BigDecimal(item_value).compareTo(eaEntertainApply.getAmount()) > 0){
//						throw new RuntimeException("报销金额:" + item_value + "超过商务接待报销标准:" + eaEntertainApply.getAmount());
//					}
//				}
//
//				if(isused.equals("1")){
//					throw new RuntimeException("招待费申请单:" + eaEntertainApply.getBillNo() + "已被使用!");
//				}else{
//					eaEntertainApply.setIsused("1");  //设置招待费申请单已使用
//					eaEntertainApplyMapper.updateById(eaEntertainApply);
//				}
//
//				//外键设置
//				entity.setBillId(eaEntertain.getId());
//				entity.setId(Uuid32Util.getUUid());
//				eaApplyDetailMapper.insert(entity);
//			}
		}
		if(eaBorrowSubtractList!=null && eaBorrowSubtractList.size()>0) {
			for(EaBorrowSubtract entity:eaBorrowSubtractList) {
				//更新借款单使用情况
				String loanBillId = entity.getLoanBillId();
				EaLoanbill eaLoanbill = eaLoanbillMapper.selectById(loanBillId);
				String isused = eaLoanbill.getIsused();   //借款单是否已使用
				if(isused.equals("1")){
					throw new RuntimeException("借款单:" + eaLoanbill.getBillNo() + "已被使用!");
				}else{
					eaLoanbill.setIsused("1");  //设置借款单已使用
					eaLoanbill.setSrcbxbillCode(eaEntertain.getBillNo());
					eaLoanbill.setSrcbxbillPk(eaEntertain.getId());
					eaLoanbill.setSrcbxbillStatus(String.valueOf(eaEntertain.getState()));
					eaLoanbillMapper.updateById(eaLoanbill);
				}

				//外键设置
				entity.setBillId(eaEntertain.getId());
				entity.setId(Uuid32Util.getUUid());
				eaBorrowSubtractMapper.insert(entity);
			}
		}
		if(eaEntertainReceptionActivities!=null && eaEntertainReceptionActivities.size()>0) {
			for(EaEntertainReceptionActivity entity:eaEntertainReceptionActivities) {
				//新增活动明细
				entity.setPid(eaEntertain.getId());
				entity.setId(Uuid32Util.getUUid());
				eaEntertainReceptionActivityMapper.insert(entity);
			}
		}

		returnMap.put("id",eaEntertain.getId());
		returnMap.put("code","0");
		returnMap.put("message","编辑成功!");
		return returnMap;
	}

	@Override
	@Transactional
	public Map<String,Object> delMain(String id) {
		Map<String,Object> retrunMap = new HashMap<String,Object>();

		//招待费报销单状态为未提交时,可以删除
		EaEntertain eaEntertain = eaEntertainMapper.selectById(id);
		String billStatus = EmptyUtil.checkNull(eaEntertain.getState());   //单据状态:0时可以删除,其它提示
		if(!billStatus.equals("0")){
			retrunMap.put("code","-1");
			retrunMap.put("message","招待费报销单为未提交时,才能被删除!");
			return retrunMap;
		}

		//获取原费用明细中的发票id
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("bill_id",eaEntertain.getId());
		List<EaBookDetail> eaBookDetailList1 = eaBookDetailMapper.selectByMap(paramsMap);
		for(EaBookDetail entity:eaBookDetailList1) {
			String fpid = entity.getFpid();  //发票id
			EaBill eaBill = eaBillMapper.selectById(fpid);
			eaBill.setIsused("0");   //设置状态为未使用
			eaBillMapper.updateById(eaBill);
		}

		//获取原费用明细中的招待费申请单id
		Map<String,Object> paramsMap2 = new HashMap<String,Object>();
		paramsMap2.put("bill_id",eaEntertain.getId());
		List<EaApplyDetail> eaApplyDetailList1 = eaApplyDetailMapper.selectByMap(paramsMap2);
		for(EaApplyDetail entity:eaApplyDetailList1) {
			String businessApplyId = entity.getBusinessApplyId();  //招待费申请单id
			EaEntertainApply eaEntertainApply = eaEntertainApplyMapper.selectById(businessApplyId);
			eaEntertainApply.setIsused("0");   //设置状态为未使用
			eaEntertainApplyMapper.updateById(eaEntertainApply);
		}

		//获取原费用明细中的借款单id
		Map<String,Object> paramsMap3 = new HashMap<String,Object>();
		paramsMap3.put("bill_id",eaEntertain.getId());
		List<EaBorrowSubtract> eaBorrowSubtractList1 = eaBorrowSubtractMapper.selectByMap(paramsMap3);
		for(EaBorrowSubtract entity:eaBorrowSubtractList1) {
			String fpid = entity.getLoanBillId();  //借款单id
			EaLoanbill eaLoanbill = eaLoanbillMapper.selectById(fpid);
			eaLoanbill.setIsused("0");   //设置状态为未使用
			eaLoanbill.setSrcbxbillCode("");
			eaLoanbill.setSrcbxbillPk("");
			eaLoanbill.setSrcbxbillStatus("");
			eaLoanbillMapper.updateById(eaLoanbill);
		}

		//删除相应的待办
		Map<String,Object> paramaMap = new HashMap<String,Object>();
		paramaMap.put("form_id",id);
		List<EaTask> eaTaskList = eaTaskMapper.selectByMap(paramaMap);
		if(eaTaskList != null && eaTaskList.size() > 0){
			EaTask eaTask = eaTaskList.get(0);

			//删除相应的工作流
			workFlowUtil.deleteWorkFlowById(eaTask.getWorkflowTaskId(),eaTask.getProcinstId());

			eaTaskMapper.deleteById(eaTask.getId());
		}

		eaBookDetailMapper.deleteByMainId(id);
		eaApplyDetailMapper.deleteByMainId(id);
		eaCostShareMapper.deleteByMainId(id);
		eaBorrowSubtractMapper.deleteByMainId(id);
		eaEntertainMapper.deleteById(id);
		Map<String,Object> activitiParams = new HashMap<String,Object>();
		activitiParams.put("pid",eaEntertain.getId());
		eaEntertainReceptionActivityMapper.deleteByMap(activitiParams);

		retrunMap.put("code","0");
		retrunMap.put("message","删除成功!");
		return retrunMap;
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			//将相应发票状态改成未使用
			Map<String,Object> paramsMap = new HashMap<String,Object>();
			paramsMap.put("bill_id",id);
			List<EaBookDetail> eaBookDetailList = eaBookDetailMapper.selectByMap(paramsMap);
			if(eaBookDetailList != null && eaBookDetailList.size() > 0){
				for (EaBookDetail eaBookDetail: eaBookDetailList) {
					String fpid = eaBookDetail.getFpid();   //发票id
					EaBill eaBill = eaBillMapper.selectById(fpid);
					eaBill.setIsused("0");  //设置发票未使用
					eaBillMapper.updateById(eaBill);
				}
			}

			eaBookDetailMapper.deleteByMainId(id.toString());
			eaApplyDetailMapper.deleteByMainId(id.toString());
			eaCostShareMapper.deleteByMainId(id.toString());
			eaBorrowSubtractMapper.deleteByMainId(id.toString());
			eaEntertainMapper.deleteById(id);
		}
	}

	//查询招待费报销单详情
	@Override
	public Map<String,Object> selectById(String id) {
		Map<String,Object> queryWrapper = new HashMap<String,Object>();
		queryWrapper.put("id",id);  //招待费报销单id
		queryWrapper.put("page",0);
		queryWrapper.put("limit",0);
		List<EaEntertain> eaEntertainList = eaEntertainMapper.selecteaEntertains(queryWrapper);
		Map<String,Object> returnMap = new HashMap<String,Object>();

		if(eaEntertainList.size() > 0){
			for (EaEntertain eaEntertain : eaEntertainList) {
				Map map = JSON.parseObject(JSON.toJSONString(eaEntertain, SerializerFeature.WriteMapNullValue), Map.class);

				if (StrUtil.isNotBlank(map.get("recPersonId").toString())){
					// 发票归属人 id转名称
					String recPersonIdName = changeIdToNameUtil.getPsnNameById(map.get("recPersonId").toString());
					map.put("recPersonIdName",recPersonIdName);
				}


				//将报销金额转为大写金额
				String eaAmt_cn = map.get("eaAmt") != null ? map.get("eaAmt").toString() : "0";
				eaAmt_cn = ConvertUpMoneyUtil.toChinese(eaAmt_cn);
				map.put("eaAmt_cn",eaAmt_cn);

				//将住宿情况_费用总计转为大写金额
				String accommodationAmt_cn = map.get("accommodationAmt") != null ? map.get("accommodationAmt").toString() : "0";
				accommodationAmt_cn = ConvertUpMoneyUtil.toChinese(accommodationAmt_cn);
				map.put("accommodationAmt_cn",accommodationAmt_cn);

				//将用餐安排_费用总计转为大写金额
				String mealArrangementAmt_cn = map.get("mealArrangementAmt") != null ? map.get("mealArrangementAmt").toString() : "0";
				mealArrangementAmt_cn = ConvertUpMoneyUtil.toChinese(mealArrangementAmt_cn);
				map.put("mealArrangementAmt_cn",mealArrangementAmt_cn);

				//将用餐安排_酒水明细（含酒水售价）转为大写金额
				String mealArrangementWineDetail_cn = map.get("mealArrangementWineDetail") != null ? map.get("mealArrangementWineDetail").toString() : "0";
				mealArrangementWineDetail_cn = ConvertUpMoneyUtil.toChinese(mealArrangementWineDetail_cn);
				map.put("mealArrangementWineDetail_cn",mealArrangementWineDetail_cn);


				//格式化时间
				Long billDate = map.get("billDate") != null ? Long.parseLong(map.get("billDate").toString()) : null;
				String billDateStr = DateFormatUtil.formatDateyyMMdd(billDate);
				map.put("billDate",billDateStr);

				//格式化时间
				Long createTime = map.get("createTime") != null ? Long.parseLong(map.get("createTime").toString()) : null;
				String createTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(createTime);
				map.put("createTime",createTimeStr);

				//格式化时间
				Long updateTime = map.get("updateTime") != null ? Long.parseLong(map.get("updateTime").toString()) : null;
				String updateTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(updateTime);
				map.put("updateTime",updateTimeStr);

				//更新时间-用于校验时效性
				map.put("time",updateTimeStr);

				//格式化时间
				Long postDate = map.get("postDate") != null ? Long.parseLong(map.get("postDate").toString()) : null;
				String postDateStr = DateFormatUtil.formatDateyyMMdd(postDate);
				map.put("postDate",postDateStr);

				//格式化时间 - 用餐安排_时间
				Long mealArrangementDate = map.get("mealArrangementDate") != null ? Long.parseLong(map.get("mealArrangementDate").toString()) : null;
				String mealArrangementDateStr = DateFormatUtil.formatDateyyMMdd(mealArrangementDate);
				map.put("mealArrangementDate",mealArrangementDateStr);

				//格式化时间 - 住宿时间
				Long accommodationTime = map.get("accommodationTime") != null ? Long.parseLong(map.get("accommodationTime").toString()) : null;
				String accommodationTimeStr = DateFormatUtil.formatDateyyMMdd(accommodationTime);
				map.put("accommodationTime",accommodationTimeStr);

				//添加图片
				String picture = map.get("picture") != null ? map.get("picture").toString() : "";
				List<Map<String,Object>> fileList = GetUrlUtil.getUrl(picture,fileurl);

				//添加图片fileList
				map.put("fileList",fileList);

				//添加相应的费用明细
				List<EaBookDetail> eaBookDetailList = eaBookDetailMapper.selectBookDetails(eaEntertain.getId());
				List<Map<String,Object>> mapList2 = new ArrayList<Map<String,Object>>();
				if(eaBookDetailList.size() > 0){
					for (EaBookDetail eaBookDetail: eaBookDetailList) {
						Map map2 = JSON.parseObject(JSON.toJSONString(eaBookDetail, SerializerFeature.WriteMapNullValue), Map.class);

						// 发票归属人 id转名称
						String fygsrName = changeIdToNameUtil.getPsnNameById(eaBookDetail.getFygsr());
						map2.put("fygsrName",fygsrName);

						//格式化时间
						Long kprq = map2.get("kprq") != null ? Long.parseLong(map2.get("kprq").toString()) : null;
						String kprqStr = DateFormatUtil.formatDateyyMMdd(kprq);
						map2.put("kprq",kprqStr);

						//格式化时间
						Long bookDate = map2.get("bookDate") != null ? Long.parseLong(map2.get("bookDate").toString()) : null;
						String bookDateStr = DateFormatUtil.formatDateyyMMdd(bookDate);
						map2.put("bookDate",bookDateStr);

						//格式化时间
						Long createTime2 = map2.get("createTime") != null ? Long.parseLong(map2.get("createTime").toString()) : null;
						String createTimeStr2 = DateFormatUtil.formatDateyyMMddHHmmss(createTime2);
						map2.put("createTime",createTimeStr2);

						//格式化时间
						Long updateTime2 = map2.get("updateTime") != null ? Long.parseLong(map2.get("updateTime").toString()) : null;
						String updateTimeStr2 = DateFormatUtil.formatDateyyMMddHHmmss(updateTime2);
						map2.put("updateTime",updateTimeStr2);

						mapList2.add(map2);
					}
				}

				//将费用明细加到eaBookDetailList节点
				map.put("eaBookDetailList",mapList2);

				//添加相应的申请单明细
				List<EaApplyDetail> eaApplyDetailList = eaApplyDetailMapper.selectByMainId(eaEntertain.getId());
				List<Map<String,Object>> mapList3 = new ArrayList<Map<String,Object>>();
				if(eaApplyDetailList.size() > 0){
					for (EaApplyDetail eaApplyDetail: eaApplyDetailList) {
						Map map3 = JSON.parseObject(JSON.toJSONString(eaApplyDetail, SerializerFeature.WriteMapNullValue), Map.class);

						//格式化时间
						Long billDate2 = map3.get("billDate") != null ? Long.parseLong(map3.get("billDate").toString()) : null;
						String billDateStr2 = DateFormatUtil.formatDateyyMMdd(billDate2);
						map3.put("billDate",billDateStr2);

						mapList3.add(map3);
					}
				}

				//将申请单明细加到eaApplyDetailList节点
				map.put("eaApplyDetailList",mapList3);

				//添加相应的费用分摊
				Map<String, Object> columnMap4 = new HashMap<String, Object>();
				columnMap4.put("bill_id", eaEntertain.getId());
				List<EaCostShare> eaCostShareList = eaCostShareMapper.selectByMap(columnMap4);
				List<Map<String,Object>> mapList4 = new ArrayList<Map<String,Object>>();
				if(eaCostShareList.size() > 0){
					for (EaCostShare eaCostShare: eaCostShareList) {
						Map map4 = JSON.parseObject(JSON.toJSONString(eaCostShare, SerializerFeature.WriteMapNullValue), Map.class);
						mapList4.add(map4);
					}
				}

				//将费用分摊加到eaCostShareList节点
				map.put("eaCostShareList",mapList4);

				//添加相应的借款单核销
				List<EaBorrowSubtract> eaBorrowSubtractList = eaBorrowSubtractMapper.selectByMainId(eaEntertain.getId());
				List<Map<String,Object>> mapList5 = new ArrayList<Map<String,Object>>();
				if(eaBorrowSubtractList.size() > 0){
					for (EaBorrowSubtract eaBorrowSubtract: eaBorrowSubtractList) {
						Map map5 = JSON.parseObject(JSON.toJSONString(eaBorrowSubtract, SerializerFeature.WriteMapNullValue), Map.class);

						//格式化时间
						Long loanBillDate = map5.get("loanBillDate") != null ? Long.parseLong(map5.get("loanBillDate").toString()) : null;
						String loanBillDateStr = DateFormatUtil.formatDateyyMMdd(loanBillDate);
						map5.put("loanBillDate",loanBillDateStr);

						mapList5.add(map5);
					}
				}

				//将借款单核销加到eaBorrowSubtractList节点
				map.put("eaBorrowSubtractList",mapList5);

				//添加相应的活动明细
				Map<String,Object> activitiParams = new HashMap<String,Object>();
				activitiParams.put("pid",eaEntertain.getId());
				List<EaEntertainReceptionActivity> eaEntertainReceptionActivities = eaEntertainReceptionActivityMapper.selectByMap(activitiParams);
				List<Map<String,Object>> mapList6 = new ArrayList<Map<String,Object>>();
				if(eaEntertainReceptionActivities.size() > 0){
					for (EaEntertainReceptionActivity eaEntertainReceptionActivity: eaEntertainReceptionActivities) {
						Map map6 = JSON.parseObject(JSON.toJSONString(eaEntertainReceptionActivity, SerializerFeature.WriteMapNullValue), Map.class);

						//格式化时间
						Long date = map6.get("date") != null ? Long.parseLong(map6.get("date").toString()) : null;
						String dateStr = DateFormatUtil.formatDateyyMMdd(date);
						map6.put("date",dateStr);

						mapList6.add(map6);
					}
				}

				//将借款单核销加到eaBorrowSubtractList节点
				map.put("eaEntertainReceptionActivities",mapList6);
				returnMap = map;
			}
		}
		return returnMap;
	}

	@Override
	@Transactional
	public Map<String, Object> audit(EaTaskVo eaTaskVo) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
		String id = EmptyUtil.checkNull(eaTaskVo.getId());   //单据id
//        String auditManId = EmptyUtil.checkNull(eaTaskVo.getAuditManId());   //审批人id
//        String auditMan = EmptyUtil.checkNull(eaTaskVo.getAuditMan());   //审批人
		String auditComment = EmptyUtil.checkNull(eaTaskVo.getAuditComment());   //审批意见
		String status = EmptyUtil.checkNull(eaTaskVo.getStatus());   //单据状态:2通过,3退回

		//获取招待费报销单-更新状态
		EaEntertain eaEntertain = eaEntertainMapper.selectById(id);
		eaEntertain.setState(Integer.parseInt(status));
		eaEntertainMapper.updateById(eaEntertain);

		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("form_id",eaEntertain.getId());
		List<EaTask> eaTaskList = eaTaskMapper.selectByMap(paramsMap);
		if(eaTaskList != null && eaTaskList.size() > 0){
			EaTask eaTask = eaTaskList.get(0);
			//eaTask.setAuditManId(auditManId);
			//eaTask.setAuditMan(auditMan);
			eaTask.setAuditComment(auditComment);
			eaTask.setStatus(status);

			try {
				if(status.equals("3") || status.equals("2")){
					eaTask.setFinishTime(new Date());   //完成时间
				}
				eaTask.setAuditTime(new Date());    //审批时间
				eaTaskMapper.updateById(eaTask);

				returnMap.put("code","0");
				returnMap.put("message","审核成功!");
				return returnMap;
			}catch (Exception e){
				returnMap.put("code","-1");
				returnMap.put("message","系统异常,审核失败!");
				return returnMap;
			}
		}else{
			returnMap.put("code","-1");
			returnMap.put("message","待办不存在!");
			return returnMap;
		}
	}

	//查询招待费报销单列表
	@Override
	public Map<String, Object> list(ListPageVo listPageVo) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Map<String,Object> returnMap = new HashMap<String,Object>();
		String page = EmptyUtil.checkNull(listPageVo.getPage());
		String limit = EmptyUtil.checkNull(listPageVo.getLimit());
		String keyword = EmptyUtil.checkNull(listPageVo.getKeyword());
		String userid = EmptyUtil.checkNull(loginUser.getId());   //用户id
		String status = EmptyUtil.checkNull(listPageVo.getStatus());   //状态
		String billNo = EmptyUtil.checkNull(listPageVo.getBillNo());   //单据编号(pc)
		String billDateBegin = EmptyUtil.checkNull(listPageVo.getBillDateBegin());   //单据日期-开始(pc)
		String billDateEnd = EmptyUtil.checkNull(listPageVo.getBillDateEnd());   //单据日期-结束(pc)
		String personId = EmptyUtil.checkNull(listPageVo.getPersonId());   //申请人/报销人/借款人/还款人(pc)
		String explainStr = EmptyUtil.checkNull(listPageVo.getCasue());   //事由(pc)
		String sysOrgCode = EmptyUtil.checkNull(listPageVo.getSysOrgCode());   //所属组织(pc)

		Map<String,Object> paramsMap = new HashMap<String,Object>();
		String keyword1 = "%" + keyword + "%";
		paramsMap.put("keyword",keyword1);
		paramsMap.put("page",(Integer.parseInt(page) -1) * Integer.parseInt(limit));
		paramsMap.put("limit",Integer.parseInt(limit));
		paramsMap.put("userid",userid);
		paramsMap.put("status",status);
		paramsMap.put("billNo",billNo);
		paramsMap.put("billDateBegin",billDateBegin);
		paramsMap.put("billDateEnd",billDateEnd);
		paramsMap.put("personId",personId);
		paramsMap.put("explainStr",explainStr);
		paramsMap.put("sysOrgCode",sysOrgCode);
		List<EaEntertain> eaEntertainList = eaEntertainMapper.selecteaEntertains(paramsMap);

		List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
		if(eaEntertainList.size() > 0){
			for (EaEntertain eaEntertain : eaEntertainList) {
				Map map = JSON.parseObject(JSON.toJSONString(eaEntertain, SerializerFeature.WriteMapNullValue), Map.class);

				//格式化时间
				Long billDate = map.get("billDate") != null ? Long.parseLong(map.get("billDate").toString()) : null;
				String billDateStr = DateFormatUtil.formatDateyyMMdd(billDate);
				map.put("billDate",billDateStr);

				//格式化时间
				Long createTime = map.get("createTime") != null ? Long.parseLong(map.get("createTime").toString()) : null;
				String createTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(createTime);
				map.put("createTime",createTimeStr);

				//格式化时间
				Long updateTime = map.get("updateTime") != null ? Long.parseLong(map.get("updateTime").toString()) : null;
				String updateTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(updateTime);
				map.put("updateTime",updateTimeStr);

				//更新时间-用于校验时效性
				map.put("time",updateTimeStr);

				//格式化时间
				Long postDate = map.get("postDate") != null ? Long.parseLong(map.get("postDate").toString()) : null;
				String postDateStr = DateFormatUtil.formatDateyyMMdd(postDate);
				map.put("postDate",postDateStr);

				//格式化时间 - 用餐安排_时间
				Long mealArrangementDate = map.get("mealArrangementDate") != null ? Long.parseLong(map.get("mealArrangementDate").toString()) : null;
				String mealArrangementDateStr = DateFormatUtil.formatDateyyMMdd(mealArrangementDate);
				map.put("mealArrangementDate",mealArrangementDateStr);

				//格式化时间 - 住宿时间
				Long accommodationTime = map.get("accommodationTime") != null ? Long.parseLong(map.get("accommodationTime").toString()) : null;
				String accommodationTimeStr = DateFormatUtil.formatDateyyMMdd(accommodationTime);
				map.put("accommodationTime",accommodationTimeStr);

				mapList.add(map);
			}
		}

		int count = 0;
		paramsMap.put("page",0);
		paramsMap.put("limit",0);
		paramsMap.put("keyword",keyword1);
		paramsMap.put("userid",userid);
		List<EaEntertain> list2 = eaEntertainMapper.selecteaEntertains(paramsMap);
		count = list2.size();

		returnMap.put("records",mapList);
		returnMap.put("total",count);
		return returnMap;
	}

	@Override
	public EaEntertain queryById(String billId) {
		return eaEntertainMapper.selectById(billId);
	}

}
